import { utils, writeFileXLSX, read } from 'xlsx'
// 导出excel
//npm i xlsx -S
const exportExcelTpl = (data, name="商品") => {
  // 导出excel模板
  const ws = utils.json_to_sheet(data);
  const wb = utils.book_new();
  utils.book_append_sheet(wb, ws, "Data");
  writeFileXLSX(wb, name+".xlsx");
}
// 写入excel
const writeExcel = (file, transForm) => {
  return new Promise((resolve, reject) => {
    const fd = new FileReader();
    fd.readAsArrayBuffer(file)
    fd.onload = () => {
      // fd.result
      const wb = read(fd.result);
      const data = utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]).map(transForm);
      resolve(data)
    }
  })

}


export {
  exportExcelTpl,
  writeExcel
}